/**
 * 수정완료시 전송할 데이터 클래스
 */
function ModifyFormData() {
	this.joinRoomSeq = null;
	this.roundingInfo = null;
	this.openYn = null;
	this.location = null;
	this.golfClubInfo = null;
	this.holeInfo = null;
	this.reserveDate = null;
	this.bookingTime = null;
	this.bookingDay = null;
	this.invitCnt = null;
	this.sex = null;
	this.age = null;
	this.aVerage = null;
	this.smokeYn = null;
	this.ment = null;
	this.rUserName = null;
	this.password = null;
	this.procStatus = null;
	this.joinYn = null;
}

function JoinRoomModify() 
{
	thisJoinRoomModify = this;
	this.TAG = " < JoinRoomModify > ";
	
	this.formData = new FormData();
	
	this.roundInfoList = null;
	
	this.roundInfoData = null;
}

JoinRoomModify.prototype = {
	getRoundInfoData: function() {
		console.log(this.TAG + "getRoundInfoData()");
		this.roundInfoList = null;
		this.roundInfoList = new Array();
		
        var mJson = new MJson();
        
         mJson.putPub(MJson.PUB.SID, "JR001");
         mJson.putPub(MJson.PUB.SERVICENAME, "getMyReservInfo");
         mJson.putPub(MJson.PUB.LISTCOUNT, "100");
         mJson.putPub(MJson.PUB.CURRPAGENO, "1");
         
         var cm = new ConnectionManager()
         
         cm.requestGet(mJson.getJsonString(), function(jsonData) {
        	if(jsonData.msp.contents.items != undefined) {
	         	if(jsonData.msp.contents.items.length != undefined) {
	 	    		for( var i=0; i<jsonData.msp.contents.items.length; i++) 
	 	    		{
	 	    			thisJoinRoomModify.roundInfoList.push(jsonData.msp.contents.items[i]);
	 	    		}
	         	} else {
	         		thisJoinRoomModify.roundInfoList.push(jsonData.msp.contents.items);
	         	}
	         	
	         	thisJoinRoomModify.initRoundInfo();
        	}
         	
         }, function(e) {
         	
         	alert("error :: " + JSON.stringify(e));
         	
         });
		
	},
	init: function() {
		console.log(this.TAG + "init()");
		
		this.formData = document.globalObj.joinRoomModifyFormData;
		
		this.roundInfoList = new Array();

		this.roundInfoData = this.formData;
		
		this.initForm();
		
		this.initEvent();
	},
	initEvent: function() {
		console.log(this.TAG + "initEvent()");
		
		/**
		 * 참여자 설정 버튼
		 */
		
		
		
		/**
		 * 수정완료 버튼
		 */
		$("#modify_complete_btn").on("click", function($event){
			console.log(thisJoinRoomModify.TAG + "수정완료 버튼 클릭!!!");
			
			var $layer_main = $("section#join_room_modify>section");

			
			
			
			/*
				Object
				bookingDay: "화"
				bookingTime: "20:12"
				golfClubInfo: "바다코스out"
				golfclubId: "sky72"
				holeInfo: 9
				reserveDate: "2012.05.22"
				reserveSeq: 433
				__proto__: Object
			 */
			
			
			var mFormData = new ModifyFormData();
			mFormData.joinRoomSeq = thisJoinRoomModify.formData.joinRoomSeq;
			mFormData.roundingInfo = "M";
			mFormData.openYn = $('[name=modify_openyn]:checked').attr("id") === "d1" ? "Y" : "N";
			
			var loc = $("#modify_location").find("p").html().split("(")[0];
			
			mFormData.location = loc === "수도권" ? "1": loc === "중부권" ? "2" : "3"; 

			mFormData.golfClubInfo = thisJoinRoomModify.roundInfoData.golfClubInfo;//$layer_main.find("dd").find("div>p").eq(1).html();
			mFormData.holeInfo = $("#modify_hole_info").val(); //thisJoinRoomModify.roundInfoData.holeInfo
			mFormData.reserveDate = thisJoinRoomModify.roundInfoData.reserveDate.split(".").join(""); //$layer_main.find("dd").find("div>p").eq(2).html().split("(")[0].split(".").join("");
			mFormData.bookingTime = thisJoinRoomModify.roundInfoData.bookingTime;
			mFormData.bookingDay = thisJoinRoomModify.roundInfoData.bookingDay;
			mFormData.invitCnt = $("#invit_cnt_selector").val();
			mFormData.sex = $('[name=sex]:checked').attr("id") === "e1" ? "A":$('[name=sex]:checked').attr("id") === "e2"?"M":"F";     // e1, e2, e3 --  무관남여 -- A M F
			mFormData.age = $("#modify_age_selector").val();
			mFormData.aVerage =  $("#modify_age_selector").val();
			mFormData.smokeYn = $('[name=smoke]:checked').attr("id") === "f1" ? "Y":"N";
			mFormData.ment = $("#modify_desc_area").val();
			mFormData.rUserName = $("#modify_r_user_name").val();
			mFormData.joinYn = $('#modify_take_checkbox').is(':checked') ? "Y":"N";
			mFormData.procStatus = $('[name=modify_proc_status]:checked').attr("id") == "a1" ? "C" : "E";
			mFormData.password = $("#modify_password").val();
			
			var checkList = new Array();

			checkList.push(mFormData.joinRoomSeq);
			checkList.push(mFormData.roundingInfo);
			checkList.push(mFormData.openYn);
			checkList.push(mFormData.location);
			checkList.push(mFormData.golfClubInfo);
			checkList.push(mFormData.holeInfo);
			checkList.push(mFormData.reserveDate);
			checkList.push(mFormData.bookingTime);
			checkList.push(mFormData.invitCnt);
			checkList.push(mFormData.sex);
			checkList.push(mFormData.age);
			checkList.push(mFormData.aVerage);
			checkList.push(mFormData.smokeYn);
			checkList.push(mFormData.ment);
			checkList.push(mFormData.rUserName);
			checkList.push(mFormData.joinYn);
			checkList.push(mFormData.procStatus);
			checkList.push(mFormData.password);
			
			console.log(checkList);
			
			var count = checkList.length;
			
			for(var i=0; i<count; i++) {
				if(checkList[i] == undefined || checkList[i] == null || checkList[i] === "")
				{
					if(mFormData.password === undefined || mFormData.password == null || mFormData.password === "") 
					{
						if(mFormData.openYn === "Y")
							continue;
						else
							return alert("모두 입력하셔야 합니다.");
					}
				}
			}
			
/*		      ,OPEN_YN = #openYn#
		      ,ROUNDING_INFO = #roundingInfo#
		      ,JOIN_YN = #joinYn#
		      ,LOCATION = #location#
		      ,GOLFCLUB_INFO = #golfClubInfo#
		      ,HOLEINFO = #holeInfo#
		      ,RESERV_DATE = #reserveDate#
		      ,BOOKING_TIME = #bookingTime#
		      ,INVIT_COUNT = #invitCnt#
		      ,SEX = #sex#
		      ,AGE = #age#
		      ,AVERAGE = #aVerage#
		      ,SMOKE_YN = #smokeYn#
		      ,MOD_DATE = to_char(sysdate,'yyyymmdd')
		      ,MENT = #ment# 
		      ,RUSER_NAME = #rUserName#
		      ,PASSWORD = #password#
		      ,PROC_STATUS = #procStatus#*/
			
			var complete = new ModifyComplete(mFormData, thisJoinRoomModify);
			complete.req();
		});
		
		/**
		 * 라운드 정보 셀렉터 체인지
		 */
		$("#modify_round_info_selector").change(function($event){
			console.log(thisJoinRoomModify.TAG + "라운드정보 바뀜!!!");
			
			var id = $event.target.value;
			
			if(id === "#") {
				thisJoinRoomModify.roundInfoData = thisJoinRoomModify.formData;
				thisJoinRoomModify.initForm();
			} else {
				
				this.roundInfoData = thisJoinRoomModify.roundInfoList[id];

				//console.log(this.roundInfoData);
				var $layer_main = $("section#join_room_modify>section");
				
				/**
				 * 홀 정보
				 */
				$("#modify_hole_info").val(thisJoinRoomModify.roundInfoData.holeInfo);
				
				/**
				 * 지역
				 */
				$layer_main.find("dd").find("div>p").eq(0).html(thisJoinRoomModify.roundInfoData.bRegionNm != undefined ? thisJoinRoomModify.roundInfoData.bRegionNm:"");
				
				/**
				 * 골프장코스
				 */
				$layer_main.find("dd").find("div>p").eq(1).html(thisJoinRoomModify.roundInfoData.golfclubId + " / " + thisJoinRoomModify.roundInfoData.golfClubInfo);
				
				/**
				 * 부킹일자
				 */
				$layer_main.find("dd").find("div>p").eq(2).html(thisJoinRoomModify.roundInfoData.reserveDate+"("+thisJoinRoomModify.roundInfoData.bookingDay+")");
				
				/**
				 * 부킹시간
				 */
				$layer_main.find("dd").find("div>p").eq(3).html(thisJoinRoomModify.roundInfoData.bookingTime);
			}
		});
		
/*    	$('#round_info_select').change(function(event) {
    		//alert(thisJoinRoomCreate.mListData[$('#round_info_select option:selected').val()].reserveDate.split('.').join(":"));
//    		alert($('#round_info_select option:selected').val());
    		//alert(thisJoinRoomCreate.mListData[$('#round_info_select option:selected').val()].golfClubInfo);
    		
    		if($('#round_info_select option:selected').val() == "-1") { // 직접입력
    			
        		golf_course_form.val("");
        		hole_info_form.val(0);
    			
        		booking_year_form.val("2012");
        		booking_month_form.val("01");
        		booking_day_form.val("01");
        		booking_hour_form.val("01");
        		booking_minute_form.val("00");        		
        		
        		thisJoinRoomCreate.refreshForm();
        		
    			return;
    		}
    	}*/
	},
	initRoundInfo: function() {
		console.log(this.TAG + "initRoundInfo()");
		
    	var option = new StringBuffer();

    	var selector = $("#modify_round_info_selector");
    	selector.empty();
    	
    	var count = this.roundInfoList.length;
    	//<option value="#" selected>스카이72 하늘코스 / 18H / 2012.05.31(수) / 07:00</option>
    	
    	option.append("<option value=# selected>");
    	option.append(this.formData.golfClubInfo);
    	option.append(" / ");
    	option.append(this.formData.holeInfo);
    	option.append(" / ");
    	option.append(this.formData.reserveDate);
    	option.append("("+this.formData.bookingDay+")");
    	option.append(" / ");
    	option.append(this.formData.bookingTime);
    	option.append("</option>");
    	
    	selector.append(option.toString());
    	option.clear();

    	for(var i=0; i<count; i++)
    	{
    		var golfclubId;
    		if( this.roundInfoList[i].golfclubId != undefined )
    			golfclubId = this.roundInfoList[i].golfclubId;
    		
    		var golfClubInfo;
    		if( this.roundInfoList[i].golfClubInfo != undefined )
    			golfClubInfo = this.roundInfoList[i].golfClubInfo;
    		
    		var holeInfo;
    		if( this.roundInfoList[i].holeInfo != undefined )
    			holeInfo = this.roundInfoList[i].holeInfo;
    		
    		var reserveDate;
    		if( this.roundInfoList[i].reserveDate != undefined )
    			reserveDate = this.roundInfoList[i].reserveDate;    		
    		
    		var bookingDay;
    		if( this.roundInfoList[i].bookingDay != undefined )
    			bookingDay = this.roundInfoList[i].bookingDay;

    		var bookingTime;
    		if( this.roundInfoList[i].bookingTime != undefined )
    			bookingTime = this.roundInfoList[i].bookingTime;
    		
    		var test_option = "<option value=" + i + ">스카이72 하늘코스 / 18H / 2012.05.31(수) / 07:00</option>"

        	option.append("<option value="+i+">");
        	option.append(golfClubInfo);
        	option.append(" / ");
        	option.append(holeInfo);
        	option.append(" / ");
        	option.append(reserveDate);
        	option.append("("+bookingDay+")");
        	option.append(" / ");
        	option.append(bookingTime);
        	option.append("</option>");

        	selector.append(option.toString());

    	}
    	
    	selector.selectmenu('refresh');
	},
	initForm: function() {
		console.log(this.TAG + "initForm()");
		
		var $layer_main = $("section#join_room_modify>section");
		
		/**
		 * 진행상태
		 */
		var $layer_procStatus = $layer_main.find("li:eq(0)").find("input");
		
		var procStatus = this.formData.procStatus;
		
		$layer_procStatus.eq(0).attr("checked", false);
		$layer_procStatus.eq(1).attr("checked", false);
		
		var procStatusValue = 0;
		
		if(procStatus === "C") procStatusValue = 0;
		else if(procStatus === "D") procStatusValue = 0;
		else if(procStatus === "E") procStatusValue = 1;
		
		$layer_procStatus.eq(procStatusValue).attr("checked", true).checkboxradio("refresh");
	
		/**
		 * 라운드 정보 데이터 연동 및 값 세팅
		 */
		this.getRoundInfoData();
		
		/**
		 * 홀 정보
		 */
		
		$("#modify_hole_info").val(this.formData.holeInfo);
		
		/**
		 * 지역
		 */
		$layer_main.find("dd").find("div>p").eq(0).html(this.formData.regionNm);
		
		/**
		 * 골프장코스
		 */
		$layer_main.find("dd").find("div>p").eq(1).html(this.formData.golfClubInfo);
		
		/**
		 * 부킹일자
		 */
		$layer_main.find("dd").find("div>p").eq(2).html(this.formData.reserveDate+"("+this.formData.bookingDay+")");
		
		/**
		 * 부킹시간
		 */
		$layer_main.find("dd").find("div>p").eq(3).html(this.formData.bookingTime);
		
		//bRegionNm
		
		/**
		 * 초대인원
		 */
		var invit_cnt_selector = $("#invit_cnt_selector");
		var invitValue = 3; //this.formData.invitCnt; --> 현재 값이 없음
		
		invit_cnt_selector.find("option[value="+invitValue+"]").attr("selected", true);
		invit_cnt_selector.change();
		
		/**
		 * 본인참여
		 */
		//$("input[type='checkbox']:first").attr("checked",true).checkboxradio("refresh");
		$("#modify_take_checkbox").attr("checked", this.formData.joinYn === "Y"? true : false).checkboxradio("refresh");
		
		/**
		 * 공개설정
		 */
		var openYn = this.formData.openYn;
		$layer_main.find("li:eq(3)").find("input").eq(0).attr("checked", false).checkboxradio("refresh");
		$layer_main.find("li:eq(3)").find("input").eq(1).attr("checked", false).checkboxradio("refresh");
		
		$layer_main.find("li:eq(3)").find("input").eq(openYn === "Y"? 0:1).attr("checked", true).checkboxradio("refresh");
		
		/*
		 *	참여조건입력하기 
		 */
		
		var $layer_take_terms = $("#take_terms_div");
		
		/**
		 * 성별   무관, 남, 여 == A, M, F
		 */
		var sexValue = this.formData.sex === "무관" ? 0 : this.formData === "남" ? 1 : 2;
		
		$layer_take_terms.find("li:eq(0)").find("input").eq(0).attr("checked", false).checkboxradio("refresh");
		$layer_take_terms.find("li:eq(0)").find("input").eq(1).attr("checked", false).checkboxradio("refresh");
		$layer_take_terms.find("li:eq(0)").find("input").eq(sexValue).attr("checked", true).checkboxradio("refresh");
		
		/**
		 * 연령
		 */
		var ageValue = null;
		var ageCd = this.formData.ageCd;
		//var ageCd = "20";
		var age_selector = $("#modify_age_selector");
		age_selector.find("option[value="+ageCd+"]").attr("selected", true).change();
		
		/**
		 * 평균타수
		 */
		var avgCd = 99; //this.formData.avgCd;
		var avg_selector = $("#modify_average");
		avg_selector.find("option[value="+avgCd+"]").attr("selected", true).change();
		
		/**
		 * 흡연여부
		 */
		var smokeValue = this.formData.smokeYn === "Y" ? 0 : 1;
		$layer_take_terms.find("li:eq(3)").find("input").eq(smokeValue).attr("checked", true).checkboxradio("refresh");
		
		/**
		 * 상세내역
		 */
		$("#modify_desc_area").val(this.formData.ment);
		
		/**
		 * 예약자 명
		 */
		$("#modify_r_user_name").val(this.formData.rUserName);

	},
	onPageShow: function() {
		console.log(this.TAG + "onPageShow()");
		this.init();
	}
};

function ModifyComplete($data, $context) {
	thisModifyComplete = this;
	this.context = $context;
	this.TAG = " < thisModifyComplete >";
	
	this.data = new ModifyFormData();
	this.data = $data;
}

/**
 * 수정완료 버튼 누를때 호출됨
 */
ModifyComplete.prototype = {
		init: function() {
			this.req();
		},
		req: function() {
			
			var $layer_main = $("section#join_room_modify>section");

			var mJson = new MJson();
			mJson.putPub(MJson.PUB.SID, "JR001");
			mJson.putPub(MJson.PUB.SERVICENAME, "updateJoinRoom");
			
			mJson.putParameter(MJson.PARAMETER.JOINROOMSEQ, this.data.joinRoomSeq);
			mJson.putParameter(MJson.PARAMETER.ROUNDINGINFO, this.data.roundingInfo); // 수정에서는 D는 없는가??
			mJson.putParameter(MJson.PARAMETER.OPENYN, this.data.openYn);
			mJson.putParameter(MJson.PARAMETER.LOCATION, this.data.location);
			mJson.putParameter(MJson.PARAMETER.GOLFCLUBINFO, this.data.golfClubInfo);
			mJson.putParameter(MJson.PARAMETER.HOLEINFO, this.data.holeInfo);
			mJson.putParameter(MJson.PARAMETER.RESERVEDATE, this.data.reserveDate );
			mJson.putParameter(MJson.PARAMETER.BOOKINGTIME, this.data.bookingTime );
			mJson.putParameter(MJson.PARAMETER.BOOKINGDAY, this.context.roundInfoData.bookingDay );
			mJson.putParameter(MJson.PARAMETER.INITCNT, this.data.invitCnt);
			mJson.putParameter(MJson.PARAMETER.SEX, this.data.sex );
			mJson.putParameter(MJson.PARAMETER.AGE, this.data.age );
			mJson.putParameter(MJson.PARAMETER.AVERAGE, this.data.aVerage );
			mJson.putParameter(MJson.PARAMETER.SMOKEYN, this.data.smokeYn );
			mJson.putParameter(MJson.PARAMETER.MENT, this.data.ment );
			mJson.putParameter(MJson.PARAMETER.RUSERNAME, this.data.rUserName );
			mJson.putParameter(MJson.PARAMETER.JOINYN, this.data.joinYn);
			mJson.putParameter(MJson.PARAMETER.PASSWORD, this.data.password);
			mJson.putParameter(MJson.PARAMETER.PROCSTATUS, this.data.procStatus);
			
			
			var cm = new ConnectionManager();
			cm.requestGet(mJson.getJsonString(), function(jsonData){
				
				if(jsonData.msp.result.code === "00000")
					$.mobile.changePage("join_room_view_my.html");
				
			}, function(e){
				alert("error:"+JSON.stringify(e));
			});
		}
}


